define(["moment"], function (moment) {
    return ["$scope","$rootScope", "$filter", "$stateParams", "config", "MMWService", "$calculater",
    function ($scope,$rootScope ,$filter, $stateParams, config, $MMWService, $calculater) {
        var datasource_format_info = {};
        $scope.init = function(){
            //引入<div class="kmi-report-panel"></div>時也要在program.json内引入css："module/moduleC/program/C00/C00.css"
            let default_modeList = [
                {name:$filter('translate')('common.report.condition.equal'), code:'0'},
                    {name:$filter('translate')('common.report.condition.nequal'), code:'1'},
                    {name:$filter('translate')('common.report.condition.contain'), code:'2'},
                    {name:$filter('translate')('common.report.condition.ncontain'), code:'3'},
                    {name:$filter('translate')('common.report.condition.exceed-equal'), code:'4'},
                    {name:$filter('translate')('common.report.condition.less-equal'), code:'5'},
                    {name:$filter('translate')('common.report.condition.exceed'), code:'6'},
                    {name:$filter('translate')('common.report.condition.less'), code:'7'}
            ];

            loadMaterialInfo();
            $scope.C00_require_condition = true;
            $scope.codeList = [
                {name:$filter('translate')('SMT2702.list.wo_no'), column_name:'MoNo', modeList:default_modeList},
                {name:$filter('translate')('SMT2702.list.old_reel_no'), column_name:'MaterialNoOld', data_type:'S', modeList:default_modeList,datasource_format_info:datasource_format_info},
                {name:$filter('translate')('SMT2702.list.new_mtrl_no'), column_name:'MaterialNoNew',data_type:"S" ,modeList:default_modeList,datasource_format_info:datasource_format_info},
                {name:$filter('translate')('SMT2702.list.copy_program'), column_name:'BurningNo',modeList:default_modeList}
            ];

            $scope.reprint_list=[];
            $scope.isSelectAll = false;
        };
 
        let loadMaterialInfo = function(){
            $MMWService.pageNumberInfoGet({
                page_mode: 'smt_material_info_get',
            }, function(itemList){
                datasource_format_info.iselectd_filed = "0";
                datasource_format_info.column_info = [$filter('translate')('SMT2702.list.mtrl_no'),$filter('translate')('SMT2702.list.mtrl_name'),$filter('translate')('SMT2702.list.mtrl_spec')];
                datasource_format_info.data_info = itemList.map(function(item){
                    return [item.material_no,item.material_name,item.material_spec];
                })
            });
        }

        $scope.back = function(){
            $scope.$root.hideMenu();
            $scope.$root.changeProgram('home', true);
        };

        $scope.search = function(condition){
            if($scope.input_value != ''){
                var content = {
                    type: 0,
                    query_condition:condition
                };
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.ic_reprint_info_get',
                    content : content,
                    success : function(data){
                        $scope.reprint_list = data.reprint_list;
                        $scope.reprint_list.forEach(function(item){
                            item.btn_mode = false;
                            item.edit = false;
                        })
                    },
                    error_callback:function(data){
                        $scope.reel_info_list = [];
                    }
                });
            } else {
                $scope.$root.showAlert($filter('translate')('B0106.msg.column1', { title:$scope.title_item.query_name }));
            };
        };

        $scope.loadProcessNo = function(item){
            $MMWService.showQueryListPNIG({
                page_mode: 'mo_process_info_get',
                header: [
                    {label:$filter('translate')('B0106.list.process_no'), code:'process_no'},
                    {label:$filter('translate')('B0106.list.process_ver'), code:'process_ver'},
                ],
                setCondition: function(conditions){
                    conditions.push({column_name:'a.MONO', query_mode:'0', value: item.wo_no, merge_condition_model:'AND'});
                    return conditions;
                },
                selectCode: item.process_no || '',
                confirm : function(selectItem){
                    item.process_no = selectItem.process_no;
                    item.process_ver = selectItem.process_ver;
                }
            });
        };

        $scope.selectAll = function(){
            if($scope.reprint_list && $scope.reprint_list.length > 0){
                $scope.isSelectAll = !$scope.isSelectAll;
                $scope.reprint_list.forEach(function(item){
                    if($scope.isSelectAll){
                        item.btn_mode = true;
                    } else {
                        item.btn_mode = false;
                    };
                });
            }
        };

        $scope.material_open = function(item){
            $MMWService.showQueryListPNIG({
                page_mode: 'smt_material_info_get',
                pageRecord:50,
                condition: [
                    {name:$filter('translate')('SMT2702.material_dialog.material_no'), column_name:'materialNo', query_mode:'0', value:'',modeList:[
                        {name:$filter('translate')('common.report.condition.equal'), code:'0'},
                        {name:$filter('translate')('common.report.condition.contain'), code:'2'}
                    ]}
                ],
                header: [
                    {label:$filter('translate')('SMT2702.material_dialog.material_no'), code:'material_no'},
                    {label:$filter('translate')('SMT2702.material_dialog.material_name'), code:'material_name'},
                    {label:$filter('translate')('SMT2702.material_dialog.material_spec'), code:'material_spec'}
                ],
                setCondition:function(conditions){
                    return conditions;
                },
                selectCode:item.material_no,
                confirm : function(selectItem){
                    item.material_no = selectItem.material_no;
                }
            });
        }

        $scope.confirm = function(){
            let reprint_checked_list = $scope.reprint_list.filter( item => item.btn_mode)
            if(reprint_checked_list.length==0){
                $rootScope.showAlert($filter('translate')('SMT2702.msg.choose_item_plz'));
            }else{
                reprint_checked_list.forEach(function(item){
                    if (item.reel_qty == undefined || item.reel_qty == '') {
                        item.reel_qty = '0';
                    }
                    if (item.burn_qty == undefined || item.burn_qty == '') {
                        item.burn_qty = '0';
                    }
                })
                 $MMWService.sendToServer({
                    uri:'ServicesSMT.Module_SMT.ic_save_print_process',
                    content : {
                        ip:config.server.ip,
                        port:config.server.port,
                        print_list:reprint_checked_list,
                        is_print:'1',
                        is_reprint:'1'
                    },
                    success: function(data){
                        if(data && data.ic_print_list && data.ic_print_list.length > 0){
                            $scope.$root.showToast($filter('translate')('SMT2702.msg.save_success'))
                            printTag(data, 0);
                        }else{
                            $rootScope.showAlert($filter('translate')('common.msg.no_data'));
                        }
                    }
                });
            }
        };

        function printTag(tags, index){
            if(tags && tags.ic_print_list && index < tags.ic_print_list.length){
                if(window.nodeRequire){
                    var exec = window.nodeRequire('child_process').exec;
                    exec(tags.ic_print_list[index].commandline, function(error, stdout, stderr){
                        if(error){
                            $rootScope.showAlert($filter('translate')('E01.msg.cantprint'));
                        }
                        index = index +1;
                        printTag(tags, index);
                    });
                }
            }
        };
    }
];});
